/**
 * Created by Luxh on 2014/6/9.
 */

function getFields(form_data) {
    var fields = [];
    fields.push('_id');

    var field_arr = form_data.fields;
    Ext.each(field_arr, function (f, index){
        if("cascade" == f.xtype){
            fields.push(f.name);
            if(f.cascade_field2){
                fields.push(f.cascade_field2);
            }
            if(f.cascade_field3){
                fields.push(f.cascade_field3);
            }
            
        }else{
            fields.push(f.name);
        }
        
    });
    fields.push('updated_at');
    fields.push('image_file_id');
    return fields;
}


function getColumns(form_data,dict) {
    var columns = [];
    columns.push({text: "_id",dataIndex: '_id',hidden: true});
    var field_arr = form_data.fields;
    Ext.each(field_arr, function (f, index){
        // console.log(f);
        // if("image_file_id"==f.name) {
        //     columns.push({text: f.field_label,dataIndex: f.name,width:150, renderer: function renderIcon(val) {
        //         var _url = "/file/download?file_id=" + val;
        //         return '<img style="height:60px;" src="' + _url + '">';
        //     }});
        // }
        if ("ckeditor"== f.xtype){

        }else if ("combobox"== f.xtype){
            if(f.use_url){
                columns.push({
                    text : f.field_label,
                    dataIndex : f.name,
                    width : 150,
                    renderer : function render(val){
                        if(dict && dict[val]){
                            return dict[val];
                        }
                        return val;
                    }
                })
            }
            else{
                var options = f.items.split("\n");
                columns.push({text:f.field_label,dataIndex:f.name,width:150, renderer: function renderIcon(val) {
                    for(var i=0;i<options.length;i++) {
                        var option = Ext.decode(options[i]);
                        if(option[f.value_field] == val){
                            return option[f.display_field];
                        }
                    }
                    return val;
                }});
            }           
            
        }else if ("cascade" == f.xtype){
            columns.push({
                text : f.field_label,
                dataIndex : f.name,
                width : 150,
                renderer : function render(val){
                    if(dict && dict[val]){
                        return dict[val];
                    }
                    return val;
                }
            });
            if(f.cascade_label2){
                columns.push({
                    text : f.cascade_label2,
                    dataIndex : f.cascade_field2,
                    width : 150,
                    renderer : function render(val){
                        if(dict && dict[val]){
                            return dict[val];
                        }
                        return val;
                    }
                });
            }
            if(f.cascade_label3){
                columns.push({
                    text : f.cascade_label3,
                    dataIndex : f.cascade_field3,
                    width : 150,
                    renderer : function render(val){
                        if(dict && dict[val]){
                            return dict[val];
                        }
                        return val;
                    }
                });
            }
        }else {
            columns.push({text: f.field_label,dataIndex: f.name,width:150});
        }
    });
    columns.push({text: "修改日期",dataIndex: 'updated_at',renderer: Ext.util.Format.dateRenderer('Y-m-d H:i:s'),width:160})
    return columns;
}


function getSearch(form_data) {
    var items = [];
    var field_arr = form_data.search_fields;
    Ext.each(field_arr, function (f2, index){
        Ext.each(form_data.fields, function (f, ind){
            if(f.name==f2.name) {
                if(f.xtype == "cascade"){
                    var cascade_params1 = {};
                    var params = {};
                    params.autoRender = true;
                    params.autoShow = true;
                    params.typeAhead = true;
                    params.queryMode = 'local';
                    params.editable = false;
                    params.xtype = "combobox";
                    // params.colspan = f.colspan;
                    params.width = f.width;
                    params.displayField = f.display_field;
                    params.valueField = f.value_field;
                    cascade_params1 = Ext.clone(params);
                    cascade_params1.name = f.name;
                    cascade_params1.fieldLabel = f.field_label;
                    cascade_params1.store = new Ext.data.Store({
                        xtype : 'store',
                        fields : [f.display_field,f.value_field],
                        autoLoad : true,
                        proxy : {
                            type : 'ajax',
                            url : f.url,
                            reader : {
                                type : 'json',
                                root : ''
                            }
                        }                                    
                    })
                    items.push(cascade_params1); 
                    var cascade_params3 = null,cascade_params2=null;
                    if(f.cascade_field2){
                        cascade_params2 = Ext.clone(params);
                        cascade_params2.name = f.cascade_field2;
                        cascade_params2.fieldLabel = f.cascade_label2;
                        cascade_params2.store = new Ext.data.Store({
                            xtype : 'store',
                            fields : [f.display_field,f.value_field],
                            autoLoad : true,
                            proxy : {
                                type : 'ajax',
                                url : f.url2,
                                reader : {
                                    type : 'json',
                                    root : ''
                                }
                            }                                    
                        })
                        items.push(cascade_params2);
                        // 多级联动
                        cascade_params1.listeners = {
                            'change': function(_this, newValue, oldValue, eOpts ){
                                if(newValue){
                                    var tmp_url = cascade_params2.store.proxy.url;
                                    tmp_url = tmp_url.substr(0,tmp_url.lastIndexOf("=")+1) + newValue;
                                    cascade_params2.store.proxy.url = tmp_url;
                                    cascade_params2.store.load();

                                    // dataForm.form.findField(f.cascade_field2).setValue("");
                                    // cascade_params3.store.load();
                                }
                            }
                        }
                    }
                    cascade_params3 = Ext.clone(params);
                    if(f.cascade_field2 && f.cascade_field3){
                        
                        cascade_params3.name = f.cascade_field3;
                        cascade_params3.fieldLabel = f.cascade_label3;
                        cascade_params3.store = new Ext.data.Store({
                            xtype : 'store',
                            fields : [f.display_field,f.value_field],
                            autoLoad : true,
                            proxy : {
                                type : 'ajax',
                                url : f.url3,
                                reader : {
                                    type : 'json',
                                    root : ''
                                }
                            }                                    
                        })
                        items.push(cascade_params3);
                        cascade_params2.listeners = {
                            'change': function(_this, newValue, oldValue, eOpts ){
                                if(newValue){
                                    var tmp_url = cascade_params3.store.proxy.url;
                                    tmp_url = tmp_url.substr(0,tmp_url.lastIndexOf("=")+1) + newValue;
                                    cascade_params3.store.proxy.url = tmp_url;
                                    cascade_params3.store.load();     
                                    // dataForm.form.findField(f.cascade_field3).setValue("");                                         
                                }
                            }
                        }
                    }
                }else{
                    items.push({fieldLabel: f.field_label,name:f.name,allowBlank: true});
                }
                
            }

        });

    });
    console.log(items);
    return items;
}


